﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcProjectFotos.Helpers;
using System.Threading;
using System.IO;

namespace MvcProjectFotos.Controllers
{
            [HandleError]
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
               return View();
            }

            public ActionResult About()
            {
                return View();
            }

            //[HttpPost]
            //public ActionResult Upload(string descripcion, HttpPostedFileBase fichero)
            //{
            //    fichero.SaveAs(Path.Combine(@"c:\Temp", Path.GetFileName(fichero.FileName)));
            //    return View();
            //}

            [HttpPost]
            public ActionResult SendImg(HttpPostedFileBase img)
            {
                var data = new byte[img.ContentLength];
                img.InputStream.Read(data, 0, img.ContentLength);
                //ruta donde guardo las iimagenes
                var path = ControllerContext.HttpContext.Server.MapPath("/Content/MisImagenes");
                var filename = Path.Combine(path, Path.GetFileName(img.FileName));
                System.IO.File.WriteAllBytes(Path.Combine(path, filename), data);
                //aqui guardo el nombre de la imagen
                ViewBag.ImageUploaded = filename;
                return View("Index");
            }

            public ActionResult Preview(string file)
            {
                var path = ControllerContext.HttpContext.Server.MapPath("/Content/MisImagenes");
                if (System.IO.File.Exists(Path.Combine(path, file)))
                {
                    return File(Path.Combine(path, file), "image/jpeg");
                }
                return new HttpNotFoundResult();
            }
        }
        
}
